/**
 * Copyright (c) 2018-2028, HQY 胡庆一 (215218393@qq.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ocm.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ocm.order.dto.AfterOrderRefuseDTO;
import com.ocm.order.dto.OrderAfterSendDTO;
import com.ocm.order.dto.WaitSendDTO;
import com.ocm.order.entity.OrderAfter;
import com.ocm.order.service.IOrderAfterService;
import com.ocm.order.service.IOrderService;
import com.ocm.order.vo.OrderAfterVO;
import com.ocm.order.wrapper.OrderAfterWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import com.ocm.common.constant.OrderAfterStatusConstant;
import com.ocm.common.constant.OrderStatusConstant;
import com.ocm.core.mp.support.Condition;
import com.ocm.core.mp.support.Query;
import com.ocm.core.secure.BladeUser;
import com.ocm.core.tool.api.R;
import com.ocm.core.tool.constant.BladeConstant;
import com.ocm.core.tool.utils.BeanUtil;
import com.ocm.core.tool.utils.DateUtil;
import com.ocm.core.tool.utils.Func;
import com.ocm.core.tool.utils.StringUtil;
import com.ocm.order.entity.Order;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ocm.core.boot.ctrl.BladeController;

import java.math.BigDecimal;

/**
 *  控制器
 *
 * @author HQY
 * @since 2022-02-02
 */
@RestController
@AllArgsConstructor
@RequestMapping("/orderafter")
@Api(value = "", tags = "接口")
public class OrderAfterController extends BladeController {

	private final IOrderAfterService orderAfterService;

	private final IOrderService orderService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入orderAfter")
	public R detail(Long id) {
		OrderAfter detail = orderAfterService.getOne(new QueryWrapper<OrderAfter>().eq("order_id",id).eq("is_deleted",0));
		return R.data(detail);
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入orderAfter")
	public R<IPage<OrderAfterVO>> list(OrderAfter orderAfter, Query query) {
		IPage<OrderAfter> pages = orderAfterService.page(Condition.getPage(query), Condition.getQueryWrapper(orderAfter));
		return R.data(OrderAfterWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入orderAfter")
	public R<IPage<OrderAfterVO>> page(OrderAfterVO orderAfter, Query query) {
		IPage<OrderAfterVO> pages = orderAfterService.selectOrderAfterPage(Condition.getPage(query), orderAfter);
		return R.data(pages);
	}

	/**
	 * 新增/修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入orderAfter")
	@Transactional
	public R save(@Valid @RequestBody OrderAfter orderAfter, BladeUser bladeUser) {
		Order order=new Order();
		if(orderAfter.getId()==null){
			order = orderService.getById(orderAfter.getOrderId());
			if(order.getStatus()!=OrderStatusConstant.ORDER_STATUS_DELIVER||!order.getCreateUser().equals(bladeUser.getUserId())){
				return R.fail("订单异常,未找到有效订单");
			}
			order = new Order();
			orderAfter.setOrderAmount(order.getFinalPrice());
			orderAfter.setShopId(order.getShopId());

		}

		order.setStatus(orderAfter.getType());

		if(!orderService.update(order,new UpdateWrapper<Order>()
			.eq("id",orderAfter.getOrderId()))){
			return R.fail("订单异常,未找到有效订单");
		}

		orderAfter.setStatus(OrderAfterStatusConstant.WAIT_DISPOSE);
		//仅保留两位小数
		orderAfter.setAmount(orderAfter.getAmount().setScale(2, BigDecimal.ROUND_DOWN));

		return R.status(orderAfterService.saveOrUpdate(orderAfter));
	}
	/**
	 * 拒绝售后
	 */
	@PostMapping("/refuse")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "拒绝售后", notes = "传入orderAfter")
	@Transactional
	public R save(@Valid @RequestBody AfterOrderRefuseDTO refuseDTO, BladeUser bladeUser) {

		OrderAfter orderAfter = new OrderAfter();
		orderAfter.setStatus(OrderAfterStatusConstant.SHOP_REFUSE);
		orderAfter.setDisposeTime(DateUtil.now());
		if(StringUtil.isNotBlank(refuseDTO.getShopRemark())){
			orderAfter.setShopRemark(refuseDTO.getShopRemark());
		}
		if(bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID)){
			return R.status(orderAfterService.update(orderAfter,new UpdateWrapper<OrderAfter>()
				.eq("id",refuseDTO.getId())
				.eq("status", OrderAfterStatusConstant.WAIT_DISPOSE)
			));
		}
		return R.status(orderAfterService.update(orderAfter,new UpdateWrapper<OrderAfter>()
			.eq("id",refuseDTO.getId())
			.eq("status", OrderAfterStatusConstant.WAIT_DISPOSE)
			.eq("shop_id",bladeUser.getShopId())
		));
	}

	/**
	 * 取消售后
	 */
	@PostMapping("/cancel")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "取消售后", notes = "传入orderId")
	@Transactional
	public R save(@Valid @RequestBody Long id, BladeUser bladeUser) {
		OrderAfter orderAfter = new OrderAfter();
		orderAfter.setStatus(OrderAfterStatusConstant.FINISH);
		orderAfter.setFinishTime(DateUtil.now());
		if(orderAfterService.update(orderAfter,new UpdateWrapper<OrderAfter>()
			.eq("order_id",id)
			.ne("status", OrderAfterStatusConstant.FINISH)
			.eq("create_user",bladeUser.getUserId())
		)){
			orderService.receiving(id,null);
		}
		return R.success("取消成功");
	}

	/**
	 * 同意退款
	 */
	@PostMapping("/refund")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "同意退款", notes = "传入orderAfter")
	@Transactional
	public R refund(BladeUser bladeUser,@RequestParam Long id ) {
		if(bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID)){
			orderService.orderAfterRefund(id,null);
			return R.success("操作成功");
		}
		orderService.orderAfterRefund(id,bladeUser.getShopId());
		return R.success("操作成功");
	}

	/**
	 * 同意退货
	 */
	@PostMapping("/waitSend")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "同意退货", notes = "WaitSendDTO")
	@Transactional
	public R refund(@Valid @RequestBody WaitSendDTO waitSendDTO, BladeUser bladeUser) {
		OrderAfter orderAfter = BeanUtil.copy(waitSendDTO, OrderAfter.class);
		orderAfter.setStatus(OrderAfterStatusConstant.WAIT_SEND_BACK);
		orderAfter.setDisposeTime(DateUtil.now());
		if(bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID)){
			return R.status(orderAfterService.update(orderAfter,new UpdateWrapper<OrderAfter>()
				.eq("status",OrderAfterStatusConstant.WAIT_DISPOSE)));
		}
		return R.status(orderAfterService.update(orderAfter,new UpdateWrapper<OrderAfter>()
			.eq("status",OrderAfterStatusConstant.WAIT_DISPOSE)
			.eq("shop_id",bladeUser.getShopId())));
	}

	/**
	 * 填写售后单号
	 */
	@PostMapping("/send")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "填写售后单号", notes = "WaitSendDTO")
	@Transactional
	public R refund(@Valid @RequestBody OrderAfterSendDTO sendDTO, BladeUser bladeUser) {
		OrderAfter orderAfter = BeanUtil.copy(sendDTO, OrderAfter.class);

		orderAfter.setStatus(OrderAfterStatusConstant.WAIT_DISPOSE_SEND_BACK);
		return R.status(orderAfterService.update(orderAfter,new UpdateWrapper<OrderAfter>().eq("create_user",bladeUser.getUserId())));
	}
	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入orderAfter")
	public R update(@Valid @RequestBody OrderAfter orderAfter) {
		return R.status(orderAfterService.updateById(orderAfter));
	}

	/**
	 * 新增或修改
	 */
//	@PostMapping("/submit")
//	@ApiOperationSupport(order = 6)
//	@ApiOperation(value = "新增或修改", notes = "传入orderAfter")
//	public R submit(@Valid @RequestBody OrderAfter orderAfter) {
//		return R.status(orderAfterService.saveOrUpdate(orderAfter));
//	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(orderAfterService.deleteLogic(Func.toLongList(ids)));
	}


}
